package com.edu.koTA07.sqlHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLHelper extends SQLiteOpenHelper {

	public static final String DB_NAME = "TakingOrder_versi12_db.sqlite";
	public static final int VERSION = 1;
	
	public static final String TABLE_SALES = "sales";
	public static final String ID_SALES = "id_sales";
	public static final String ID_PERUSAHAAN1 = "id_perusahaan";
	public static final String NAMA_SALES = "nama_sales";
	public static final String USERNAME   = "username";
	public static final String PASSWORD = "password";
	
	public static final String TABLE_BARANG = "barang";
	public static final String ID_BARANG = "id_barang";
	public static final String ID_KATEGORI_BARANG1 = "id_kategori_barang";
	public static final String NAMA_BARANG = "nama_barang";
	public static final String HARGA_BARANG   = "harga_barang";
	
	public static final String TABLE_CUSTOMER = "customer";
	public static final String ID_CUSTOMER = "id_customer";
	public static final String NAMA_TOKO = "nama_toko";
	public static final String NAMA_PEMILIK = "nama_pemilik";
	public static final String TELEPON = "telepon";
	public static final String TIPE_HARI_KUNJUNGAN = "tipe_hari_kunjungan";
	public static final String KODE_KUNJUNGAN  = "kode_kunjungan";
	public static final String ALAMAT_CUSTOMER = "alamat_customer";
	
	public static final String TABLE_PERUSAHAAN = "perusahaan";
	public static final String ID_PERUSAHAAN = "id_perusahaan";
	public static final String NAMA_PERUSAHAAN = "nama_perusahaan";
	public static final String ALAMAT_PERUSAHAAN = "alamat_perusahaan";
	
	public static final String TABLE_KATEGORI_BARANG = "kategori_barang";
	public static final String ID_KATEGORI_BARANG = "id_kategori_barang";
	public static final String NAMA_KATEGORI_BARANG = "nama_kategori_barang";
	
	public static final String TABLE_DISKON = "diskon";
	public static final String ID_DISKON = "id_diskon";
	public static final String ID_BARANG2 = "id_barang";
	public static final String BESAR_DISKON = "besar_diskon";
	public static final String JUMLAH_BARANG = "jumlah_barang";
	
	public static final String TABLE_PESANAN = "pesanan";
	public static final String ID_PESANAN = "id_pesanan";
	public static final String ID_SALES1 = "id_sales";
	public static final String ID_CUSTOMER2 = "id_customer";
	public static final String ID_STATUS_KUNJUNGAN1 = "id_status_kunjungan";
	public static final String TANGGAL_TRANSAKSI = "tanggal_transaksi";
	
	public static final String TABLE_PESANAN_DETAIL = "pesanan_detail";
	public static final String ID_PESANAN_DETAIL = "id_pesanan_detail";
	public static final String ID_BARANG1 = "id_barang";
	public static final String ID_PESANAN1 = "id_pesanan";
	public static final String KUANTITAS_PESANAN = "kuantitas_pesanan";
	public static final String TOTAL = "total";
	public static final String POTONGAN = "potongan";
	
	public static final String TABLE_STATUS_KUNJUNGAN = "status_kunjungan";
	public static final String ID_STATUS_KUNJUNGAN = "id_status_kunjungan";
	public static final String ID_CUSTOMER1 = "id_customer";
	public static final String ID_PESANAN2 = "id_pesanan";
	public static final String STATUS = "status";
	public static final String TGL_STATUS = "tgl_status";
	public static final String NAMA_PENGHANDLE = "nama_penghandle";
	
	public SQLHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createTable(db);
	}

	private void createTable(SQLiteDatabase db) {
		db.execSQL("create table " + TABLE_CUSTOMER + " ( " + 
				ID_CUSTOMER + " text primary key not null, " +
				NAMA_TOKO + " text, " +
				NAMA_PEMILIK + " text, " + 
				TELEPON + " text, " +
				TIPE_HARI_KUNJUNGAN + " text, " +
				KODE_KUNJUNGAN + " text, " +
				ALAMAT_CUSTOMER + " text " +
				");");
		db.execSQL("create table " + TABLE_BARANG + " ( " + 
				ID_BARANG + " text primary key not null, " +
				ID_KATEGORI_BARANG1 + " text, " + 
				NAMA_BARANG + " text, " +
				HARGA_BARANG + " integer " +
				");");
		db.execSQL("create table " + TABLE_KATEGORI_BARANG + " ( " + 
				ID_KATEGORI_BARANG + " text primary key not null, " +
				NAMA_KATEGORI_BARANG + " text " +
				");");
		db.execSQL("create table " + TABLE_SALES + " ( " + 
				ID_SALES+ " text primary key not null, " +
				ID_PERUSAHAAN1 + " text, " + 
				NAMA_SALES + " text, " +
				USERNAME + " text, " +
				PASSWORD + " text " +
				");");
		db.execSQL("create table " + TABLE_PESANAN+ " ( " + 
				ID_PESANAN + " integer primary key autoincrement not null, " +
				ID_SALES1 + " text not null, " +
				ID_CUSTOMER2 + " text not null, " +
				ID_STATUS_KUNJUNGAN1 + " integer not null, " +
				TANGGAL_TRANSAKSI + " text " +
				");");		
		db.execSQL("create table " + TABLE_PERUSAHAAN + " ( " + 
				ID_PERUSAHAAN + " text primary key not null, " +
				NAMA_PERUSAHAAN + " text, " +
				ALAMAT_PERUSAHAAN + " text " +
				");");
		db.execSQL("create table " + TABLE_DISKON+ " ( " + 
				ID_DISKON + " text primary key not null, " +
				ID_BARANG2 + " text not null, " +
				BESAR_DISKON + " integer, " +
				JUMLAH_BARANG + " integer " +
				");");
		db.execSQL("create table " + TABLE_PESANAN_DETAIL+ " ( " + 
				ID_PESANAN_DETAIL + " integer primary key autoincrement not null," +
				ID_BARANG1 + " text not null, " +
				ID_PESANAN1 + " integer not null," +
				KUANTITAS_PESANAN + " integer, " +
				TOTAL + " float, " +
				POTONGAN + " float" +
				");");
		db.execSQL("create table " + TABLE_STATUS_KUNJUNGAN + " ( " + 
				ID_STATUS_KUNJUNGAN + " integer primary key autoincrement not null," +
				ID_CUSTOMER1 + " text not null, " +
				ID_PESANAN2 + " integer not null," +
				STATUS + " boolean," +
				TGL_STATUS + " text," +
				NAMA_PENGHANDLE + " text" +
				");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exists "+ TABLE_SALES);
		db.execSQL("drop table if exists "+ TABLE_BARANG);
		db.execSQL("drop table if exists "+ TABLE_CUSTOMER);
		db.execSQL("drop table if exists "+ TABLE_PERUSAHAAN);
		db.execSQL("drop table if exists "+ TABLE_KATEGORI_BARANG);
		db.execSQL("drop table if exists "+ TABLE_DISKON);
		db.execSQL("drop table if exists "+ TABLE_PESANAN);
		db.execSQL("drop table if exists "+ TABLE_PESANAN_DETAIL);
		db.execSQL("drop table if exists "+ TABLE_STATUS_KUNJUNGAN );
		
		this.onCreate(db);
	}
}
